﻿/// <reference path="new_common_scripts.js" />
/// <reference path="new_XrmPage_vsdoc.js" />
/// <reference path="new_jquery.js" />
/// <reference path="new_json.js" />
/// <reference path="new_XrmServiceToolkit.js" />

var quote = {
    addOnChangeFunctions: function () {
        Xrm.Page.getAttribute("new_format_of_page").addOnChange(quote.new_format_of_page_onchange);
        Xrm.Page.getAttribute("new_paper_type").addOnChange(quote.new_paper_type_onchange);
    },

    onLoad: function () {
        quote.addOnChangeFunctions();

        quote.new_format_of_page_onchange();
        quote.new_paper_type_onchange();
    },

    new_format_of_page_onchange: function () {
        var new_format_of_page = common.getAttrKey("new_format_of_page");

        common.setDisabledControl("new_format_of_page_other", (new_format_of_page != 100000003));
        common.setRequiredControl("new_format_of_page_other", (new_format_of_page == 100000003));
        if (new_format_of_page != 100000003)
            common.setAttrValue("new_format_of_page_other", null);
    },

    new_paper_type_onchange: function () {
        var new_paper_type = common.getAttrKey("new_paper_type");

        common.setDisabledControl("new_paper_type_other", (new_paper_type != 100000002));
        common.setRequiredControl("new_paper_type_other", (new_paper_type == 100000002));
        if (new_paper_type != 100000002)
            common.setAttrValue("new_paper_type_other", null);
    },

    export_quote: function () {
        if (Xrm.Page.data.entity.getIsDirty() == true) {
            alert("На форме есть несохраненные данные. Сохраните Предложение и повторите еще раз.");
            return;
        }

        var id = Xrm.Page.data.entity.getId();

        if (id != null) {
            common.showPreLoader();
            common.setAttrValue("new_exportquote", true);
            Xrm.Page.data.save().then(quote.export_quote_successCallback, quote.export_quote_errorCallback);
            return;
        }
        else {
            alert("Сохраните Предложение.");
        }
    },

    export_quote_enabled: function () {
        return true;
    },

    export_quote_successCallback: function () {
        common.hidePreLoader();
        $("#NotesTab").click();
        alert("КП сформировано");
    },

    export_quote_errorCallback: function (result) {
        common.setAttrValue("new_exportquote", false);
        common.hidePreLoader();
    }
}